home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / PLOT3D.C < prev    next >
Text File  |  1990-09-09  |  11KB  |  419 lines

  1. /*
  2.    This file includes miscellaneous plot functions and logic
  3.    for 3D, used by lorenz.c and line3d.c
  4.    By Tim Wegner and Marc Reinig.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include "fractint.h"
  9. #include "fractype.h"
  10.  
  11. /* Use these palette indices for red/blue - same on ega/vga */
  12. #define BLUE        1
  13. #define RED        2
  14. #define MAGENTA     3
  15.  
  16. int whichimage;
  17. extern int fractype;
  18. extern int mapset;
  19. extern int xadjust;
  20. extern int yadjust;
  21. extern int xxadjust;
  22. extern int yyadjust;
  23. extern int xshift;
  24. extern int yshift;
  25. extern char MAP_name[];
  26. extern int init3d[];
  27. extern int xdots;
  28. extern int ydots;
  29. extern int colors;
  30. extern unsigned char dacbox[256][3];
  31. extern void (*standardplot)();
  32.  
  33. int xxadjust1;
  34. int yyadjust1;
  35. int eyeseparation = 0;
  36. int glassestype = 0;
  37. int xshift1;
  38. int yshift1;
  39. int xtrans = 0;
  40. int ytrans = 0;
  41. int red_local_left;
  42. int red_local_right;
  43. int blue_local_left;
  44. int blue_local_right;
  45. int red_crop_left   = 4;
  46. int red_crop_right  = 0;
  47. int blue_crop_left  = 0;
  48. int blue_crop_right = 4;
  49. int red_bright        = 80;
  50. int blue_bright      = 100;
  51.  
  52. /* Bresenham's algorithm for drawing line */
  53. void draw_line (int X1, int Y1, int X2, int Y2, int color)
  54.  
  55. {                  /* uses Bresenham algorithm to draw a line */
  56.   int dX, dY;                        /* vector components */
  57.   int row, col,
  58.       final,                       /* final row or column number */
  59.       G,                   /* used to test for new row or column */
  60.       inc1,            /* G increment when row or column doesn't change */
  61.       inc2;               /* G increment when row or column changes */
  62.   char pos_slope;
  63.   extern int xdots,ydots;
  64.   extern int (*plot)();
  65.  
  66.   dX = X2 - X1;                    /* find vector components */
  67.   dY = Y2 - Y1;
  68.   pos_slope = (dX > 0);                    /* is slope positive? */
  69.   if (dY < 0)
  70.     pos_slope = !pos_slope;
  71.   if (abs (dX) > abs (dY))                /* shallow line case */
  72.   {
  73.     if (dX > 0)             /* determine start point and last column */
  74.     {
  75.       col = X1;
  76.       row = Y1;
  77.       final = X2;
  78.     }
  79.     else
  80.     {
  81.       col = X2;
  82.       row = Y2;
  83.       final = X1;
  84.     }
  85.     inc1 = 2 * abs (dY);           /* determine increments and initial G */
  86.     G = inc1 - abs (dX);
  87.     inc2 = 2 * (abs (dY) - abs (dX));
  88.     if (pos_slope)
  89.       while (col <= final)    /* step through columns checking for new row */
  90.       {
  91.     (*plot) (col, row, color);
  92.     col++;
  93.     if (G >= 0)                 /* it's time to change rows */
  94.     {
  95.       row++;         /* positive slope so increment through the rows */
  96.       G += inc2;
  97.     }
  98.     else                         /* stay at the same row */
  99.       G += inc1;
  100.       }
  101.     else
  102.       while (col <= final)    /* step through columns checking for new row */
  103.       {
  104.     (*plot) (col, row, color);
  105.     col++;
  106.     if (G > 0)                 /* it's time to change rows */
  107.     {
  108.       row--;         /* negative slope so decrement through the rows */
  109.       G += inc2;
  110.     }
  111.     else                         /* stay at the same row */
  112.       G += inc1;
  113.       }
  114.   }  /* if |dX| > |dY| */
  115.   else                              /* steep line case */
  116.   {
  117.     if (dY > 0)                /* determine start point and last row */
  118.     {
  119.       col = X1;
  120.       row = Y1;
  121.       final = Y2;
  122.     }
  123.     else
  124.     {
  125.       col = X2;
  126.       row = Y2;
  127.       final = Y1;
  128.     }
  129.     inc1 = 2 * abs (dX);           /* determine increments and initial G */
  130.     G = inc1 - abs (dY);
  131.     inc2 = 2 * (abs (dX) - abs (dY));
  132.     if (pos_slope)
  133.       while (row <= final)    /* step through rows checking for new column */
  134.       {
  135.     (*plot) (col, row, color);
  136.     row++;
  137.     if (G >= 0)                  /* it's time to change columns */
  138.     {
  139.       col++;      /* positive slope so increment through the columns */
  140.       G += inc2;
  141.     }
  142.     else                      /* stay at the same column */
  143.       G += inc1;
  144.       }
  145.     else
  146.       while (row <= final)    /* step through rows checking for new column */
  147.       {
  148.     (*plot) (col, row, color);
  149.     row++;
  150.     if (G > 0)                  /* it's time to change columns */
  151.     {
  152.       col--;      /* negative slope so decrement through the columns */
  153.       G += inc2;
  154.     }
  155.     else                      /* stay at the same column */
  156.       G += inc1;
  157.       }
  158.   }
  159. }  /* draw_line */
  160.  
  161. /* use this for continuous colors later */
  162. void plot3dsuperimpose16b(int x,int y,int color)
  163. {
  164.    int tmp;
  165.    if (color != 0)           /* Keeps index 0 still 0 */
  166.    {
  167.       color = colors - color; /*  Reverses color order */
  168.       color = color / 4;
  169.       if(color == 0)
  170.      color = 1;
  171.    }
  172.    color = 3;
  173.    tmp = getcolor(x,y);
  174.  
  175.    /* map to 4 colors */
  176.    if(whichimage == 1) /* RED */
  177.    {
  178.       if(red_local_left < x && x < red_local_right)
  179.      putcolor(x,y,color|tmp);
  180.    }
  181.    else if(whichimage == 2) /* BLUE */
  182.    {
  183.       if(blue_local_left < x && x < blue_local_right)
  184.       {
  185.      color = color <<2;
  186.      putcolor(x,y,color|tmp);
  187.       }
  188.    }
  189. }
  190.  
  191. void plot3dsuperimpose16(int x,int y,int color)
  192. {
  193.    int tmp;
  194.  
  195.    tmp = getcolor(x,y);
  196.  
  197.    if(whichimage == 1) /* RED */
  198.    {
  199.       color = RED;
  200.       if(tmp > 0 && tmp != color)
  201.      color = MAGENTA;
  202.       if(red_local_left < x && x < red_local_right)
  203.      putcolor(x,y,color);
  204.    }
  205.    else if(whichimage == 2) /* BLUE */
  206.    {
  207.       if(blue_local_left < x && x < blue_local_right)
  208.       {
  209.      color = BLUE;
  210.      if(tmp > 0 && tmp != color)
  211.         color = MAGENTA;
  212.      putcolor(x,y,color);
  213.       }
  214.    }
  215. }
  216.  
  217. void plot3dsuperimpose256(x,y,color)
  218. {
  219.    int tmp;
  220.    if (color != 0)           /* Keeps index 0 still 0 */
  221.    {
  222.       color = colors - color; /*  Reverses color order */
  223.       color = 1 + color / 18; /*  Maps colors 1-255 to 15 even ranges */
  224.    }
  225.    tmp = getcolor(x,y);
  226.    /* map to 16 colors */
  227.    if(whichimage == 1) /* RED */
  228.    {
  229.       if(red_local_left < x && x < red_local_right)
  230.       /* Overwrite prev Red don't mess w/blue */
  231.      putcolor(x,y,color|(tmp&240));
  232.    }
  233.    else if(whichimage == 2) /* BLUE */
  234.    {
  235.       if(blue_local_left < x && x < blue_local_right)
  236.       {
  237.      /* Overwrite previous blue, don't mess with existing red */
  238.      color = color <<4;
  239.      putcolor(x,y,color|(tmp&15));
  240.       }
  241.    }
  242. }
  243.  
  244. void plotIFS3dsuperimpose256(x,y,color)
  245. {
  246.    int tmp;
  247.    if (color != 0)           /* Keeps index 0 still 0 */
  248.    {
  249.       /* my mind is fried - lower indices = darker colors is EASIER! */
  250.       color = colors - color; /*  Reverses color order */
  251.       color = 1 + color / 18; /*  Looks weird but maps colors 1-255 to 15
  252.                      relatively even ranges */
  253.    }
  254.    tmp = getcolor(x,y);
  255.    /* map to 16 colors */
  256.    if(whichimage == 1) /* RED */
  257.    {
  258.       if(red_local_left < x && x < red_local_right)
  259.      putcolor(x,y,color|tmp);
  260.    }
  261.    else if(whichimage == 2) /* BLUE */
  262.    {
  263.       if(blue_local_left < x && x < blue_local_right)
  264.       {
  265.      color = color <<4;
  266.      putcolor(x,y,color|tmp);
  267.       }
  268.    }
  269. }
  270.  
  271. void plot3dalternate(x,y,color)
  272. {
  273.    /* lorez high color red/blue 3D plot function */
  274.    /* if which image = 1, compresses color to lower 128 colors */
  275.  
  276.    /* my mind is STILL fried - lower indices = darker colors is EASIER! */
  277.    color = colors - color;
  278.    if((whichimage == 1) && !((x+y)&1)) /* - lower half palette */
  279.    {
  280.       if(red_local_left < x && x < red_local_right)
  281.      putcolor(x,y,color>>1);
  282.    }
  283.    else if((whichimage == 2) && ((x+y)&1) ) /* - upper half palette */
  284.    {
  285.       if(blue_local_left < x && x < blue_local_right)
  286.      putcolor(x,y,(color>>1)+(colors>>1));
  287.    }
  288. }
  289.  
  290. plot_setup()
  291. {
  292.    double d_red_bright, d_blue_bright;
  293.    int i;
  294.  
  295.    /* set funny glasses plot function */
  296.    switch(glassestype)
  297.    {
  298.    case 1:
  299.       standardplot = plot3dalternate;
  300.       break;
  301.    case 2:
  302.       if(colors == 256)
  303.      if (fractype != IFS3D)
  304.         standardplot = plot3dsuperimpose256;
  305.      else
  306.         standardplot = plotIFS3dsuperimpose256;
  307.       else
  308.      standardplot = plot3dsuperimpose16;
  309.       break;
  310.    default:
  311.       standardplot = putcolor;
  312.       break;
  313.    }
  314.  
  315.    xshift1 = xshift = (XSHIFT * (double)xdots)/100;
  316.    yshift1 = yshift = (YSHIFT * (double)ydots)/100;
  317.  
  318.    if(glassestype)
  319.    {
  320.       red_local_left   =    (red_crop_left         * (double)xdots)/100.0;
  321.       red_local_right  =    ((100 - red_crop_right)  * (double)xdots)/100.0;
  322.       blue_local_left  =    (blue_crop_left         * (double)xdots)/100.0;
  323.       blue_local_